#include <cstdio>
#include <algorithm>
#include <cstring>
typedef long long ll;
using namespace std;
const int MOD=1e9+7;
ll qPow(ll a,int n){
    ll ans=1;
    while(n){
        if(n%2){
            ans=(ans*a)%MOD;
        }
        a=(a*a)%MOD;
        n/=2;
    }
    return ans;
}
int main(void){
    int t;
    ll n;
    int k;
    scanf("%d",&t);
    while(t--){
        scanf("%lld%d",&n,&k);
        ll ans=0;
        for(int i=1;i<=n;i++){
            ans=(ans+qPow(i,k))%MOD;
        }
        printf("%lld\n",ans);
    }
    return 0;
}